import pytest

from tools.handle_data import Data
from tools.handle_faker import HandleFaker
from tools.handle_request import HandleRequests
from tools.handle_path import ying_ye_zhi_zhao_jpg


# 1、定义一个前置夹具，随机生成一个未注册的手机号
@pytest.fixture()
def get_unregistered_phone():
    # 1、用faker库生成一个未注册的手机号
    handle_faker_fixture = HandleFaker()
    unregistered_phone_fixture = handle_faker_fixture.get_unregistered_phone()
    mobile_phone_dict_fixture = {"phone": unregistered_phone_fixture}
    setattr(Data, "unregistered_phone", unregistered_phone_fixture)
    yield mobile_phone_dict_fixture, unregistered_phone_fixture


# 2、定义一个前置夹具，随机生成一个未注册的用户名
@pytest.fixture()
def get_unregistered_user_name():
    # 1、用faker库生成一个未注册的用户名
    handle_faker_fixture = HandleFaker()
    unregistered_user_name_fixture = handle_faker_fixture.get_unregistered_user_name()
    setattr(Data, "unregistered_user_name", unregistered_user_name_fixture)
    yield unregistered_user_name_fixture


# 3、定义一个前置夹具，上传图片(始终上传同一张图片)，得到一个营业执照图片地址字符串
@pytest.fixture()
def file_upload_get_license_url():
    # 营业执照图片
    file_name = 'YingYeZhiZhao.jpg'
    # 2、发起一个请求，得到未注册手机号码的验证码
    handle_requests_fixture = HandleRequests()
    # 发送请求
    file_upload_response_fixture = handle_requests_fixture.send_request(method='post',
                                                                        url='/file/upload',
                                                                        data=None,
                                                                        file_name=file_name)
    license_url_fixture = file_upload_response_fixture.get('data')
    setattr(Data, "license_url", license_url_fixture)
    yield license_url_fixture


# 4、定义一个前置夹具，用未注册的手机号发起请求，得到一个短信验证码
@pytest.fixture()
def get_verification_code(get_unregistered_phone):
    mobile_phone_dict_fixture, unregistered_phone_fixture = get_unregistered_phone
    # 2、发起一个请求，得到未注册手机号码的验证码
    handle_requests_fixture = HandleRequests()
    verification_code_response_fixture = handle_requests_fixture.send_request(method='get',
                                                                              url='/verificationCode/message',
                                                                              data=mobile_phone_dict_fixture,
                                                                              file_name=None)
    sms_verification_code_fixture = verification_code_response_fixture.get('data')
    setattr(Data, "verification_code", sms_verification_code_fixture)
    yield sms_verification_code_fixture


# 4、定义一个前置夹具，发起一个注册请求，得到用户名、用户id
@pytest.fixture()
def user_register_get_user_id_user_name(get_unregistered_phone, get_verification_code, get_unregistered_user_name):
    # 调用夹具得到未注册的手机号
    mobile_phone_dict_fixture, unregistered_phone_fixture = get_unregistered_phone

    # 调用夹具得到短信验证码
    sms_verification_code_fixture = get_verification_code

    # 调用夹具得到未注册的用户名
    unregistered_user_name_fixture = get_unregistered_user_name

    # 4、发起一个注册请求，得到用户名、用户id
    register_data_fixture = {
        "phone": unregistered_phone_fixture,
        "pwd": "Aa123456",
        "rePwd": "Aa123456",
        "userName": unregistered_user_name_fixture,
        "verificationCode": sms_verification_code_fixture
    }
    handle_requests_fixture = HandleRequests()
    user_register_response_fixture = handle_requests_fixture.send_request(method='post',
                                                                          url='/user/register',
                                                                          data=register_data_fixture,
                                                                          file_name=None)
    # 得到用户id
    user_id_fixture = user_register_response_fixture['data']['id']
    # 得到用户名
    user_name_fixture = user_register_response_fixture['data']['userName']
    setattr(Data, "user_id", user_id_fixture)
    setattr(Data, "user_name", user_name_fixture)
    yield user_id_fixture, user_name_fixture


# 5、定义一个前置夹具，发起一个登录请求，得到token
@pytest.fixture()
def user_login_get_token_user_id(user_register_get_user_id_user_name):
    # 调用夹具得到未注册的用户名
    user_id_fixture, user_name_fixture = user_register_get_user_id_user_name
    login_data_fixture = {
        "pwd": "Aa123456",
        "userName": user_name_fixture
    }

    handle_requests_fixture = HandleRequests()
    user_login_response_fixture = handle_requests_fixture.send_request(method='post',
                                                                       url='/user/login',
                                                                       data=login_data_fixture,
                                                                       file_name=None)
    # 得到token
    token_fixture = user_login_response_fixture['data']['token_info']['token']
    setattr(Data, "token", token_fixture)
    setattr(Data, "user_id", user_id_fixture)
    yield token_fixture, user_id_fixture
